package ucn.dmf83.sc.workshop1.group3.UILayer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import ucn.dmf83.sc.workshop1.group3.ControlLayer.AddressBookCtrl;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.Copy;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.DVD;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.Loan;
import ucn.dmf83.sc.workshop1.group3.DomainLayer.Person;

public class PeopleMenu {
	private static AddressBookCtrl controller;
	
	private static void clear() {
		int i = 0;
		while(i < 25)
		{
			System.out.println("");
			i++;
		}
	}
	
	private static void showHeader(String menuName) {
		clear();
   	 	System.out.println("------------------------------------------------");
        System.out.println("-                                              -");
        System.out.println("-    AddressBook v0.1 by Group3 Inc.           -");
        System.out.println("-                                              -");
        System.out.println("------------------------------------------------");

        System.out.println("\n\n\f *** " + menuName +  " *** \n\n");
	}
	
    public static void showMenu() {
    	showHeader("AddressBook Menu");
        int choice = printPeopleMenu();
        
        switch(choice) {
        case 1:
        	findPerson();
        case 2:
            addPerson();
        case 3:
        	listAllPeople();
        case 4:
       	 	MainMenu.showMenu();
        default:
            System.out.println("Invalid selection, please try again!");
            showMenu();
        }
   }
   
   public static void showMenu(AddressBookCtrl controller1)
   {
	   	controller = controller1;
	   	showMenu();
   }
   
   public static int printPeopleMenu() {
       System.out.println(" 1. Search");
       System.out.println(" 2. Add person");
       System.out.println(" 3. List all");
       System.out.println(" 4. Return");

       System.out.print("\n \n Please type in the number of your requested option: ");
       
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
       
		int i = 0;
 		
 		try {
 			i = Integer.parseInt(data);
 		}
 		catch(Exception e) {
 			e.printStackTrace();
 		}
 		
        return i;
   }
   
   public static void findPerson() {
	   showHeader("Find person");
	   System.out.println("Enter name of user: \n");
	   
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		ArrayList<Person> people = controller.getPeople(data);
		
		for(Iterator<Person> it = people.iterator(); it.hasNext();) {
			Person p = it.next();
			System.out.println(p.getId() + "\t" + p.getName() + "  " + p.getAddress() + ", " + p.getPostalCode() + " " + p.getCity() + "\t" + p.getPhone()); 
		}
		
		if(people.size() != 0)
		{
			System.out.println(" Please select person by ID: "); 
			
			data = null;
			try {
				data = br.readLine();
			} catch (IOException e) { 
				e.printStackTrace();
			}
			
			int i = 0;
	 		
	 		try {
	 			i = Integer.parseInt(data);
	 		}
	 		catch(Exception e) {
	 			e.printStackTrace();
	 		}
	 		
	 		showPerson(i);
		}else{
			System.out.println("No users matched the description, returning to main menu...");
			showMenu();
		}
		
   }
   
   public static void showPerson(int id) {
	   showHeader("Personal Records  -  #" + id);
	   Person p = controller.getPerson(id);
	   
	   
	   if(id == 0) {
		   System.out.println("Invalid ID, returning to AddressBook...");
		   try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		   showMenu();
	   }
	   
	   System.out.println("Name: " + p.getName());
	   System.out.println("Address: " + p.getAddress() + ", " + p.getPostalCode() + " " + p.getCity());
	   System.out.println("Phone: " + p.getPhone());
	   
	   
	   System.out.println("\n\n Enter one of the following numbers for interaction: \n");
	   System.out.println("1 - Return Loan \n2 - Add Loan\n3 - Edit record\n4-Remove record");
	   
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
	   String data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		int i = 0;
		
		try {
			i = Integer.parseInt(data);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		switch(i) {
		case 1:
			returnLoan(id);
		case 2:
			addLoan(id);
		case 3:
			editRecord(id);
		case 4:
			controller.removePerson(p.getAddress(), p.getCity(), p.getPostalCode());
			showMenu();
		case 5:
			showMenu();
		default:
			showPerson(id);
		}
   }
   
   public static void editRecord(int id)
   {
	   showHeader("Edit record #" + id);
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	   Person person = controller.getPerson(id);
	   String data;
	   
	   System.out.println("Current name: " + person.getName() + "\nEnter new name:\n");
	   data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		person.setName(data);
		
		System.out.println("Current address: " + person.getAddress() + "\nEnter new address:\n");
		data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
		person.setAddress(data);
			
		System.out.println("Current city: " + person.getCity() + "\nEnter new city:\n");
		   data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			person.setCity(data);
			
		System.out.println("Current phone: " + person.getPhone() + "\nEnter new phone:\n");
		data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		person.setPhone(data);
		
		System.out.println("Current Postal Code: " + person.getName() + "\nEnter new Postal Code:\n");
		   data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			person.setPostalCode(Integer.parseInt(data));
			
		showMenu();
   } 
   
   
   public static void addPerson()
   {
	   showHeader("Add new record");
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	   String data;
	   
	   System.out.println("Enter name: ");
	   data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String name = data;
		
		System.out.println("Enter address: ");
		data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
		String address = data;
			
		System.out.println("Enter city:\n");
		   data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		String city = data;
			
		System.out.println("Enter phone:\n");
		data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String phone = data;
		
		System.out.println("Enter Postal Code:\n");
		   data = null;
			try {
				data = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		int postCode = 0;	
		try{
			postCode = Integer.parseInt(data);
		}catch(Exception e) {
			
		}
			
		controller.addPerson(name, address, city, postCode, phone);
		showMenu();
   } 
   
   
   
   public static void returnLoan(int personID)
   {
	   showHeader("Return loan");
	   Person person = controller.getPerson(personID);
	   
	   for(Iterator<Loan> it = person.getLoans().iterator(); it.hasNext();) {
		   Loan loan = it.next();
		   System.out.println(loan.getId() + "\t" + loan.getCopy().getOriginal().getTitle() + "\t" + loan.getBorrowDate() + " " + loan.getPeriod());
	   }
	   
	   System.out.println("Type in the ID of the loan you want to return: ");
	   
	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
	   String data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		int i = 0;
		
		try {
			i = Integer.parseInt(data);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		controller.returnLoan(person, i);
	   
   }
   
   public static void addLoan(int personID)
   {
	   showHeader("Loan");
	   Person person = controller.getPerson(personID);

	   BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
	   
		System.out.println("Enter the SerialNum of the copy: ");
		
		String data;
		data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Copy copy = controller.getCopy(data);
		
		System.out.println("Enter the borrow date: ");
		
		data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String borrowDate = data;
		
		System.out.println("Enter the loan period: ");
		
		data = null;
		try {
			data = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		controller.addLoan(person, borrowDate, Integer.parseInt(data), copy);
	   
   }
   
   public static void listAllPeople() {
	   showHeader("List of known people");
	   ArrayList<Person> people = controller.getPeople();
	   
	   for(Iterator<Person> it = people.iterator(); it.hasNext();) {
		   Person person = it.next();
		   
		   System.out.println(person.getName() + "\t" + person.getAddress() + ", " + person.getPostalCode() + " " + person.getCity() + "\t" + person.getPhone()); 
	   }
	   Scanner sc = new Scanner(System.in);
	   System.out.println("Press Enter to return to AddressBook menu");
	   sc.nextLine();
	   showMenu();
	   sc.close();
   }
}
